﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.SqlClient;
using System.Configuration;

using NHibernate;
using NHibernate.Tool.hbm2ddl;
using NCommet.Core;
using NCommet.Modules.Dao;

namespace NCommet.Testing
{
    /// <summary>
    /// Summary description for GenericUnitTester
    /// </summary>
    [TestClass]
    public class GenericUnitTester
    {
        private static TestContext TestContext;

        public GenericUnitTester()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            GenericUnitTester.TestContext = testContext;
            NCommet.Core.Configuration.Configurator.Configure();
        }

        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [DataSource("HierarchyCreation")]
        [Description("Tests the creation of complex tree hierarchies")]
        [TestMethod]
        public void CreateComplexHierarchyTest()
        {

            bool bSaveRoot = Convert.ToBoolean(TestContext.DataRow["SaveRootFirst"]);
            bool bSaveChild = Convert.ToBoolean(TestContext.DataRow["SaveChildFirst"]);
            string Name = TestContext.DataRow["Name"].ToString();

            //Create the item and assert that is has been saved
            Item root = NCommet.Core.Item.Create(Name, "TestItemType", bSaveRoot);

            for (int i = 1; i < 10; i++)
            {
                if (!TestContext.DataRow.Table.Columns.Contains("Child" + i) || TestContext.DataRow["Child" + i].ToString() == String.Empty)
                    break;
                root.Children.Add(Item.Create(TestContext.DataRow["Child" + i].ToString(), "TestItemType", bSaveChild));
                for (int j = 1; j < 10; j++)
                {
                    if (!TestContext.DataRow.Table.Columns.Contains("Child" + i + "-" + j) || TestContext.DataRow["Child" + i + "-" + j].ToString() == String.Empty)
                        break;
                    root.Children[i - 1].Children.Add(Item.Create(TestContext.DataRow["Child" + i + "-" + j].ToString(), "TestItemType", bSaveChild));
                }
            }

            root.Save();

            Item Actual = Item.Get(root.Id);
            Console.WriteLine(Actual.Id + " : " + Actual.Name);
            //Print the hierarchy to the Console
            foreach (Item FirstLevelChild in Actual.Children)
            {
                Console.WriteLine("\t" + FirstLevelChild.Id + " : " + FirstLevelChild.Name);
                foreach (Item SecondLevelChild in FirstLevelChild.Children)
                {
                    Console.WriteLine("\t\t" + SecondLevelChild.Id + " : " + SecondLevelChild.Name);
                }
            }
            Assert.Inconclusive("Check the Console to see if the test was successfull");
        }



        [TestMethod]
        public void RecreateSchema()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                SchemaExport se = new SchemaExport(NHibernateSessionManager.Instance.Config);

                //se.SetOutputFile("C:\\test.sql");
                se.SetDelimiter("\nGO\n\n");
                se.Execute(true, true, false, true, conn, Console.Out);
            }
        }

    }
}
